// pages/member/qr/index.js
var e = getApp(), t = e.requirejs("core"), a = e.requirejs("wxParse/wxParse"), i = e.requirejs("biz/diypage"), s = e.requirejs("jquery");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgSrc:"https://s3.ax1x.com/2021/01/18/s6zJ00.png",
    nickname:"",
    avatar:"",
    ani:{}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(p) {
    let that = this;
    // 验证用户授权信息
    e.checkAuth(), that.setData({
        options: p
    });
    t.get("member/qr",{},function(res) {
      that.setData({
        nickname: res.nickname,
        avatar: res.avatar
      });
    })
    t.get("commission/poster",{},function(req){
      if (0 == req.error){
        let sid = req.poster[0]['id'];
        if(sid>0){
          t.get("commission/poster/getimage",{id:sid},function(rea){
            that.setData({
              imgSrc: rea.poster
            });
          })
        }
      }
    })
  },
  // 长按保存
  saveImage(e) {
    let that = this
    wx.showActionSheet({
      itemList: ['保存到相册'],
      success(res) {
        let url = e.currentTarget.dataset.url;
        wx.getSetting({
          success: (res) => {
            console.log(res);
            if (!res.authSetting['scope.writePhotosAlbum']) {   // 未授权
              wx.authorize({
                scope: 'scope.writePhotosAlbum',
                success: () => {
                  that.saveImgSuccess(url);
                },
                fail: (res) => {
                  console.log(res);
                  wx.showModal({
                    title: '保存失败',
                    content: '请开启访问手机相册的权限',
                    success(res) {
                      wx.openSetting()
                    }
                  })
                }
              })
            } else {  // 已授权
              that.saveImgSuccess(url);
            }
          },
          fail: (res) => {
            console.log(res);
          }
        })
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  // 同意授权保存到相册
  saveImgSuccess(url) {
    wx.getImageInfo({
      src: url,  // 通过getImageInfo将src转换成改图片的本地路径，给saveImageToPhotosAlbum使用
      success: (res) => {
        console.log(res)
        let path = res.path;
        wx.saveImageToPhotosAlbum({
          filePath: path,   // filePath路径不能是网络图片路径
          success: (res) => {
            console.log(res);
            wx.showToast({
              title: '已保存到相册',
            })
          },
          fail: (res) => {
            console.log(res);
          }
        })
      },
      fail: (res) => {
        console.log(res);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'linear',
      delay: 0
    });
    animation.translateY(-10).step()
    this.setData({
      ani:animation.export()
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // var animation = wx.createAnimation({
    //   duration: 500,
    //   timingFunction: 'ease',
    //   delay: 1000
    // });
    // animation.translate(0, -10).step()
    // this.setData({
    //   ani:animation.export()
    // })
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})